<?php

namespace app\api\controller\business;

use think\Db;
use think\Log;

/**
 * 添加
 * Class Adds
 * @package app\api\controller\business
 * 
 * @method boolean process($uid, $id, $typs, $remark)				处理【激活未达标 | 流失客户 | 超标客户】
 * @method boolean actNewlyGiftActivity($id, $uid, $remark)			确认【处理】活动达标
 * @method boolean issueAllot($ids, $uid)							礼品发放分配 - 业务操作
 * @method boolean ac_feedback($id, $uid, $remark)					确认反馈
 * @method boolean visit($option, $ui)								售后回访
 */
class Adds
{

	/**
	 * 实例对象
	 * @var object
	 */
	protected static $instance;

	/**
	 * @var \app\commom\model\User
	 */
	protected $userModel = null;

	/**
	 * @var \app\commom\model\QuestionContent
	 */
	protected $quesModel = null;

	/**
	 * @var \app\commom\model\ActivityTake
	 */
	protected $takesModel = null;

	/**
	 * @var \app\commom\model\Admin
	 */
	protected $adminModel = null;

	/**
	 * @var \app\commom\model\TerminalActvieStands
	 */
	protected $acteModel = null;

	/**
	 * @var \app\commom\model\TransLostCustomer
	 */
	protected $lostModel = null;

	/**
	 * @var \app\commom\model\TransLimitsWarn
	 */
	protected $outModel = null;

	public function __construct()
	{
		$this->userModel 	= new \app\common\model\User;
		$this->adminModel 	= new \app\common\model\Admin;
		$this->takesModel 	= new \app\common\model\ActivityTake;
		$this->quesModel 	= new \app\common\model\QuestionContent;
		$this->acteModel 	= new \app\common\model\TerminalActvieStands;
		$this->outModel 	= new \app\admin\model\TransLimitsWarn;
		$this->lostModel 	= new \app\common\model\TransLostCustomer;
	}

	/**
	 * 实例化
	 */
	public static function instance()
	{
		if (is_null(self::$instance)) {
			self::$instance = new static();
		}
		return self::$instance;
	}

	/**
	 * 处理【激活未达标 | 流失客户 | 超标客户】
	 * 区分类型 1 激活未达标 2  流失客户 3 超标客户
	 */
	public function process($uid, $id, $typs, $remark)
	{
		if ($uid && $id && $typs) {
			$dao = null;
			switch ($typs) {
				case 1:
					$dao = $this->acteModel;
					break;
				case 2:
					$dao = $this->lostModel;
					break;
				case 3:
					$dao = $this->outModel;
					break;
			}
			if ($dao) {
				$finds = $dao->where("id", $id)->find();
				if ($finds->process == 2) {
					Db::startTrans();
					try {
						$user = $this->userModel->where("id", $uid)->find();
						$update = [
							"process_uid" 		=> $uid,
							"process_admin_id" 	=> $user->admin_id,
							"process"			=> 1,
							"process_time"		=> time(),
							"remark"			=> $remark ? $remark : "处理人" . $user->nickname
						];
						$ret = $dao->where("id", $id)->update($update);
						if ($ret) {
							Db::commit();
						}
					} catch (\Exception $e) {
						Db::rollback();
						return false;
					}
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 绑定礼品
	 */
	public function bindGift($uid, $id, $code)
	{
		if (!$uid && !$code && !$id) {
			return false;
		}

		$where = [];
		$admin_id = $this->userModel->where("id", $uid)->value('admin_id');
		$g_id = Db::name("gift")->where("code", $code)->value("id");
		if ($admin_id && $g_id) {
			$where = ["admin_id" => $admin_id, "gift_id" => $g_id];
			$find = Db::name("gift_count")->where($where)->find();
			if ($find) {
				$nums = $find["use_number"] + 1;
				if ($nums <= $find["stock_number"]) {
					Db::startTrans();
					try {
						$update = ["gift_code" => $code, "update_time" => time()];
						$ret = $this->takesModel->where('id', $id)->update($update);
						if ($ret) {
							$updateGif["use_number"] = $nums;
							$re = Db::name("gift_count")->where($where)->update($updateGif);
							if ($re) {
								Db::commit();
							}
						}
					} catch (\Exception $e) {
						Db::rollback();
						Log::write($e->getMessage());
						return false;
					}
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 礼品发放分配
	 */
	public function issueAllot($ids, $uid)
	{
		if (!$uid) {
			return false;
		}
		Db::startTrans();
		try {
			$update = [
				"allot_uid" 	=> $uid,
				"is_allot" 		=> 1,
				"allot_time"	=> time()
			];
			$ret = $this->takesModel->where("id", $ids)->update($update);
			if ($ret) {
				Db::commit();
			}
		} catch (\Exception $e) {
			Db::rollback();
			return false;
		}
		return true;
	}

	/**
	 * 确认新人有礼活动
	 */
	public function actNewlyGiftActivity($id, $uid, $remark)
	{
		if (!$id && !$uid) {
			return false;
		}
		$admin_id = $this->userModel->where("id", $uid)->value("admin_id");
		if ($admin_id) {
			Db::startTrans();
			try {
				$update = [
					"process" 		=> 1,
					"remark" 		=> $remark,
					"process_id"	=> $admin_id,
					"process_time"	=> time(),
					"process_uid"	=> $uid
				];
				$ret = $this->takesModel->where("id", $id)->update($update);
				if ($ret) {
					Db::commit();
				}
			} catch (\Exception $e) {
				Db::rollback();
				return false;
			}
			return true;
		}
	}

	/**
	 * 确认反馈
	 */
	public function ac_feedback($id, $uid, $remark)
	{
		if ($uid) {
			$admin_id = $this->userModel->where("id", $uid)->value("admin_id");
			if ($admin_id) {
				Db::startTrans();
				try {
					$update = [
						"process" 		=> 1,
						"process_des" 	=> $remark,
						"process_id"	=> $admin_id,
						"process_time"	=> time(),
						"process_uid"	=> $uid
					];
					$ret = $this->quesModel->where("id", $id)->update($update);
					if ($ret) {
						Db::commit();
					}
				} catch (\Exception $e) {
					Db::rollback();
					return false;
				}
				return true;
			}
		} else {
			return false;
		}
	}

	/**
	 * 客服回访
	 * @param  $name 角色组ID
	 */
	public function visit($option, $uid)
	{
		$option["process_id"] = 0;
		$option["process"]	= 1;
		$option["process_time"] = time();
		$option["create_time"] 	= time();
		$option["process_id"] = $this->userModel->where("id", $uid)->value('admin_id');
		Db::startTrans();
		try {
			$id = $this->quesModel->insertGetId($option);
			if ($id) {
				Db::commit();
			}
		} catch (\Exception $e) {
			Db::rollback();
			return false;
		}
		return true;
	}
}
